#include "UserManager.h"
#include "User.h"
#include "Configuration.h"

IUserManager*	UserManager::_instance = NULL;

UserManager::UserManager()
{
	//this->LoadUser(); pas utile on load pas tout les users, en memoire on a que ceux connectes
}

IUserManager*	UserManager::GetUserManager()
{
	if (UserManager::_instance == NULL)
		UserManager::_instance = new UserManager();
	return UserManager::_instance;
}

IUser*			UserManager::GetUser(unsigned int id)
{
	std::list<IUser*>::iterator	iteCurrent = this->_user.begin();
	std::list<IUser*>::iterator	iteEnd = this->_user.end();

	while (iteCurrent != iteEnd)
	{
		if ((*iteCurrent)->GetId() == id)
			return *iteCurrent;
		++iteCurrent;
	}
	return NULL;
}

IUser*			UserManager::GetUser(std::string& login)
{
	std::list<IUser*>::iterator	iteCurrent = this->_user.begin();
	std::list<IUser*>::iterator	iteEnd = this->_user.end();

	while (iteCurrent != iteEnd)
	{
		if ((*iteCurrent)->GetLogin() == login)
			return *iteCurrent;
		++iteCurrent;
	}
	return NULL;
}

unsigned int	UserManager::LoadUser()
{
	this->UnloadUser();
	IDatabase::DbResult* result = Configuration::GetConfiguration()->GetDatabase()->SelectAllUser();
	for (unsigned int idx = 0; idx < result->size(); idx++)
		this->_user.push_back(new User((*result->at(idx))["id"]->GetValueInt(), (*result->at(idx))["login"]->GetValueString(), (*result->at(idx))["password"]->GetValueString()));
	return 0;
}

IUser*	UserManager::LoadUser(const unsigned int id)
{
	this->UnloadUser(id);
	IDatabase::DbResult* result = Configuration::GetConfiguration()->GetDatabase()->SelectUser(id);
	if (result->size() == 1)
	{
		IUser* user = new User(id, (*result->at(0))["login"]->GetValueString(), (*result->at(0))["password"]->GetValueString());
		this->_user.push_back(user);
		return user;
	}
	////else //error
	return NULL;
}

unsigned int	UserManager::UnloadUser()
{
	std::list<IUser*>::iterator	iteCurrent = this->_user.begin();
	std::list<IUser*>::iterator	iteEnd = this->_user.end();

	while (iteCurrent != iteEnd)
	{
		Configuration::GetConfiguration()->GetDatabase()->UpdateUser((*iteCurrent)->GetId(), (*iteCurrent)->GetLogin(), (*iteCurrent)->GetPassword());
		++iteCurrent;
	}
	this->_user.clear();
	return 0;
}

unsigned int	UserManager::UnloadUser(const unsigned int id)
{
	std::list<IUser*>::iterator	iteCurrent = this->_user.begin();
	std::list<IUser*>::iterator	iteEnd = this->_user.end();

	while (iteCurrent != iteEnd)
	{
		if ((*iteCurrent)->GetId() == id)
		{
			Configuration::GetConfiguration()->GetDatabase()->UpdateUser((*iteCurrent)->GetId(), (*iteCurrent)->GetLogin(), (*iteCurrent)->GetPassword());
			this->_user.erase(iteCurrent);
			return 0;
		}
		++iteCurrent;
	}
	return -1;
}

IUser*	UserManager::CreateUser(const std::string& login, const std::string& password)
{
	unsigned int id = Configuration::GetConfiguration()->GetDatabase()->CreateUser(login, password);
	IUser *user = new User(id, login, password);
	this->_user.push_back(user);
	return user;
}

unsigned int	UserManager::DeleteUser()
{
	//remove de la base tout les users
	Configuration::GetConfiguration()->GetDatabase()->DeleteAllUser();
	//delete des users
	std::list<IUser*>::iterator	iteCurrent = this->_user.begin();
	std::list<IUser*>::iterator	iteEnd = this->_user.end();

	while (iteCurrent != iteEnd)
	{
		delete *iteCurrent;
		++iteCurrent;
	}
	this->_user.clear();
	return 0;
}

unsigned int	UserManager::DeleteUser(const unsigned int id)
{
	//remove de la base un user
	Configuration::GetConfiguration()->GetDatabase()->DeleteUser(id);
	IUser* user = this->GetUser(id);
	this->_user.remove(user);
	delete user;
	return 0;
}

bool			UserManager::UserExists(const std::string& login)
{
	IDatabase::DbResult*	result = Configuration::GetConfiguration()->GetDatabase()->SelectUser(login);

	if (result->size())
		return true;
	return false;
}